package ua.com.minus1.lookbattle;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import ua.com.minus1.lookbattle.menu.ClicksActionView;
import ua.com.minus1.lookbattle.models.CacheFolder;
import ua.com.minus1.lookbattle.models.CommonResult;
import ua.com.minus1.lookbattle.models.Look;
import ua.com.minus1.lookbattle.net.HttpManager;
import ua.com.minus1.lookbattle.net.ImageManager;
import ua.com.minus1.lookbattle.net.URLManager;
import ua.com.minus1.lookbattle.utils.AppPreferences;
import ua.com.minus1.lookbattle.utils.BundleValues;

/**
 * Created with IntelliJ IDEA.
 * User: Igorek
 * Date: 9/26/12
 * Time: 10:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class LookDetailsFragment extends SherlockFragment implements ConvertDialogFragment.Callbacks {

    private HttpManager mHttpManager;
    private ImageManager mImageManager;
    private AppPreferences mPreferences;
    private int mCatId;
    private int mLookId;
    Look mLook;
    private ViewHolder mViewHolder;
    private ViewGroup mContainer;
    private ClicksActionView mClicksActionView;

    public interface Callbacks{
        void onConvertPopup();
    }

    public static LookDetailsFragment newInstance(int catId, int lookId) {
        LookDetailsFragment f = new LookDetailsFragment();

        Bundle args = new Bundle();
        args.putInt(BundleValues.CAT_ID, catId);
        args.putInt(BundleValues.LOOK_ID, lookId);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mHttpManager = new HttpManager(getActivity(), CacheFolder.HTTP_MY_LOOKS);
        mImageManager = new ImageManager(getActivity(), CacheFolder.IMAGES_MY_LOOKS);
        mPreferences = new AppPreferences(getActivity());

        mCatId = getArguments().getInt(BundleValues.CAT_ID, 0);
        mLookId = getArguments().getInt(BundleValues.LOOK_ID, 0);

        if(mClicksActionView != null)
            mClicksActionView.setClicks(mPreferences.getClicks());

        mHttpManager.loadClass(URLManager.getLookInfoURL(mPreferences.getUUID(), mCatId, mLookId), Look.class, new HttpManager.HttpResultHandler<Look>() {
            public void onResultLoadComplete(Look result) {
                Log.i(LookActivity.class.getName(), "Look: " + result.lookId + " request result: " + result.toString());
                mLook = result;
                updateLook();
            }
        });
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContainer = (ViewGroup) inflater.inflate(R.layout.fragment_look_details, mContainer, false);
        setupView();
        return mContainer;
    }

    private void setupView() {
        mViewHolder = new ViewHolder();
        mViewHolder.creationDate = (TextView) mContainer.findViewById(R.id.lookInfoCreationDate);
        mViewHolder.country = (TextView) mContainer.findViewById(R.id.lookInfoCountry);
        mViewHolder.globalPlace = (TextView) mContainer.findViewById(R.id.loookRatesGlobalPlace);
        mViewHolder.localPlace = (TextView) mContainer.findViewById(R.id.lookRatesLocalPace);
        mViewHolder.winnings = (TextView) mContainer.findViewById(R.id.lookRatesWinnings);
        mViewHolder.battles = (TextView) mContainer.findViewById(R.id.lookRatesBattles);
        mViewHolder.bonuses = (TextView) mContainer.findViewById(R.id.lookBonuses);
        mViewHolder.image = (ImageView) mContainer.findViewById(R.id.lookInfoImage);
        mViewHolder.progress = mContainer.findViewById(R.id.lookInfoProgress);

    }

    public void showConvertDialog() {
        FragmentManager fm = getFragmentManager();

        ConvertDialogFragment convertDialog = ConvertDialogFragment.newInstance(mCatId, mLookId, mLook.bonuses, mPreferences.getClicks());
        convertDialog.setTargetFragment(this, 0);
        convertDialog.show(fm, "convert_dialog");
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.look_details, menu);
        mClicksActionView = (ClicksActionView) menu.findItem(R.id.menu_look_details_clicks).getActionView();
        if(mPreferences != null)
            mClicksActionView.setClicks(mPreferences.getClicks());
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.convert_clicks:
                showConvertDialog();
                return true;
            case R.id.remove_look:
                removeLook();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void removeLook() {
        mHttpManager.loadClass(URLManager.getDeleteLookURL(mPreferences.getUUID(), mLookId, mCatId), CommonResult.class, new HttpManager.HttpResultHandler<CommonResult>() {
            public void onResultLoadComplete(CommonResult result) {
                Log.i(LookDetailsFragment.class.getName(), "delete look result: " + result.toString());


                Intent intent = new Intent();
                intent.putExtra(Intent.EXTRA_DATA_REMOVED, true);
                getActivity().setResult(Activity.RESULT_OK, intent);
                getActivity().finish();
            }
        });
    }

    public void onConvert(int bonuses, int clicks) {
        mViewHolder.bonuses.setText(String.valueOf(bonuses));
        mPreferences.setClicks(clicks);
        mClicksActionView.setClicks(clicks);
    }


    private void updateLook() {
        mViewHolder.bonuses.setText(String.valueOf(mLook.bonuses));
        mViewHolder.globalPlace.setText(String.valueOf(mLook.globalPlace));
        mViewHolder.localPlace.setText(String.valueOf(mLook.localPlace));
        mViewHolder.battles.setText(String.valueOf(mLook.battles));
        mViewHolder.winnings.setText(String.valueOf(mLook.winnings));
        mViewHolder.creationDate.setText(mLook.date);
        if(mLook.countryId != -1)
            mViewHolder.country.setText(CountriesFactory.getCountry(getActivity(), mLook.countryId).name);
        mViewHolder.progress.setVisibility(View.VISIBLE);
        mViewHolder.image.setImageBitmap(null);
        mImageManager.loadBitmap(mLook.image, mViewHolder.image, new ImageManager.BitmapLoadListener() {
            public void onLoadComplete(Bitmap bitmap) {
                mViewHolder.progress.setVisibility(View.GONE);
            }
        });
    }

    private class ViewHolder{

        public TextView creationDate;
        public TextView country;
        public TextView globalPlace;
        public TextView localPlace;
        public TextView winnings;
        public TextView battles;
        public TextView bonuses;
        public ImageView image;
        public View progress;
    }
}
